<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Sms\SmsSendLogRepository;
use Illuminate\Http\Request;

// 分销商
class MessageSendController extends BaseController
{
    private $repoist;

    public function __construct(SmsSendLogRepository $model)
    {
        $this->repoist = $model;
    }

    public function store(Request $request)
    {
        return $this->repoist->add($request);
    }

    public function index(Request $request)
    {
        $where = [];
        if (!empty($request->get('name'))) {
            $where['like']['sms_tmp_name'] = $request->get('name');
        }
        if ($request->has('sms_use_type') && $request->get('sms_use_type') !== '') {
            $where['where']['sms_use_type'] = $request->get('sms_use_type');
        }
        if (!empty($request->get('order_id'))) {
            $where['where']['order_id'] = $request->get('order_id');
        }
        if (!empty($request->get('phone'))) {
            $where['like']['phone'] = $request->get('phone');
        }
        if (!empty($request->get('goods_name'))) {
            $where['like']['goods_name'] = $request->get('goods_name');
        }
        if (($request->has('status'))) {
            $where['where']['status'] = $request->get('status');
        }
        if (!empty($request->get('product_name'))) {
            $where['like']['product_name'] = $request->get('product_name');
        }
        if (!empty($request->get('order_status'))) {
            $where['like']['order_status_desc'] = $request->get('order_status');
        }
        if (!empty($request->get('express_status'))) {
            $where['like']['express_status_desc'] = $request->get('express_status');
        }
        if (!empty($request->get('reason'))) {
            $where['like']['reason'] = $request->get('reason');
        }
        if (!empty($request->get('is_express'))) {
            if ($request->get('is_express') == 1) {
                $where['where'][] = ['express_status', '>', 0];
                $where['where'][] = ['order_status', '=', 0];
            }
            if ($request->get('is_express') == 2) {
                $where['where'][] = ['express_status', '=', 0];
                $where['where'][] = ['order_status', '>', 0];
            }
        }
        if (!empty($request->get('pageNum'))) {
            $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
            $where['limit']['number'] = $request->get('pageSize');
        }

        if ($request->user->role_id != 1) {
            $where['where']['shop_id'] = $request->user->shop_id;
        }
        if (!empty($request->get('dateRange')) && empty($request->get('end_nums'))) {
            $dateRange = [date('Y-m-d', strtotime($request['dateRange'][0])), date('Y-m-d', strtotime($request['dateRange'][1])) . ' 23:59:59'];
            $where['between'] = ['created_at' => $dateRange];
        }
        // var_dump($where);
        $where['select'] = ['order_id', 'sms_id', 'sms_tmp_name', 'dist_name', 'dist_id', 'sms_tmp_id', 'order_status', 'order_status_desc', 'is_phone', 'goods_name', 'delay', 'goods_id', 'shop_id', 'id', 'created_at', 'updated_at', 'status', 'is_send', 'content', 'phone', 'product_name', 'express_status', 'express_status_desc', 'reason'];
        $data = $this->repoist->index($where);
        if (!empty($data) && $data['code'] == 200) {
            $data['statusList'] = config('card.smsSendLogsStatus');
            $data['useTypeList'] = config('card.smsUseType');
        }
        return $data;
    }

    public function save(Request $request)
    {
        if (!$request->get('id')) {
            return $this->returnMsg(1, '请传递id参数');
        }
        // $where['select'] = ['shop_id' => $request->user->shop_id];
        // $where['select'] = ['shop_id' => $request->user->shop_id];
        $where['select'] = ['shop_id' => $request->user->shop_id];
        // var_dump($request->get('id'));
        if (is_array($request->get('id'))) {
            $where['in']['value'] = $request->get('id');
            $where['in']['field'] = 'id';
        }
        if (is_string($request->get('id'))) {
            $where['select']['id'] = $request->get('id');
        }
        // var_dump($where);
        // die();
        return $this->repoist->edit($where, $request);
    }

    public function show(Request $request, $id)
    {
        $where = ['id' => $id, 'shop_id' => $request->user->shop_id];
        //查询一条数据
        $return = $this->repoist->getOne($where, ['*']);
        if (!empty($return)) {
            $return['status'] = (int)$return->status;
            $return['is_phone'] = (int)$return->is_phone;
        }

        return $this->returnMsg(200, '查询成功', $return);
    }

    public function listss(Request $request)
    {
        $where = [];
        if (!empty($request->get('name'))) {
            $where['where'] = ['name' => $request->get('name')];
        }
        $where['where']['shop_id'] = $request->user->shop_id;
        // var_dump($where);
        $return = $this->repoist->lists($where, 1, 99999);
        return $return;
    }

    public function destroy(Request $request, $id)
    {
        if (!$id) {
            return $this->returnMsg(1, '请传递参数');
        }
        $where = [
            'id' => $id,
            'shop_id' => $request->user->shop_id
        ];
        return $this->repoist->del($where);
    }

    public function updateStatus(Request $request, $id)
    {
        $where = [
            'id' => $id,
            'shop_id' => $request->user->shop_id
        ];
        $update = [
            'status' => $request->get('status'),
        ];
        return $this->repoist->edit($where, $request);
    }
}
